package com.ladybug.flow.common.exception.controller;

import com.ladybug.flow.common.exception.customexceptions.ErrorPageException;
import com.ladybug.flow.common.exception.enums.ErrorEnum;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;

/**
* @description: TODO
* @author long
* @date 2022/10/17 17:14
* @version 1.0
*/
@Controller
public class ErrorConfig implements ErrorController {
    private final static String ERROR_PATH = "/error" ;

    public String getErrorPath() {
        return ERROR_PATH;
    }

    @RequestMapping(ERROR_PATH)
    public void errorPathHandler(HttpServletResponse response) {
        //抛出ErrorPageException异常，方便被ExceptionHandlerConfig处理
        ErrorEnum errorEnum;
        switch (response.getStatus()) {
            case 404:
                errorEnum = ErrorEnum.NOT_FOUND;
                break;
            case 403:
                errorEnum = ErrorEnum.FORBIDDEN;
                break;
            case 401:
                errorEnum = ErrorEnum.UNAUTHORIZED;
                break;
            case 400:
                errorEnum = ErrorEnum.BAD_REQUEST;
                break;
            case 10003:
                errorEnum = ErrorEnum.USER_NAME_OR_PWD_ERROR;
            default:
                errorEnum = ErrorEnum.UNKNOWN;
                break;
        }
        throw new ErrorPageException(errorEnum);
    }
}
